<template>
  <div class="app-container">
    <el-row style="margin-bottom: 1%">
      <el-col :span="8">
        <div class="titleClass">积分总额</div>
        <div class="titleContentClass">
          {{ productList.queryTotalPerformance || "0.00" }}
        </div>
      </el-col>
      <el-col :span="8">
        <div class="titleClass">贡献值</div>
        <div class="titleContentClass">
          {{ productList.userMoney ? productList.userMoney.allmoney : "0.00" }}
        </div>
      </el-col>
      <el-col :span="8">
        <div class="titleClass">累计释放</div>
        <div class="titleContentClass">
          {{ productList.userMoney ? productList.userMoney.maxmoney : "0.00" }}
        </div>
      </el-col>
    </el-row>
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户ID" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入用户ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入用户姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邀请人" prop="invitee">
        <el-input
          v-model="queryParams.invitee"
          placeholder="请输入邀请人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大区业绩" prop="regionalPerformance">
        <el-input
          v-model="queryParams.regionalPerformance"
          placeholder="请输入大区业绩"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小区业绩" prop="communityPerformance">
        <el-input
          v-model="queryParams.communityPerformance"
          placeholder="请输入小区业绩"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分享码" prop="share">
        <el-input
          v-model="queryParams.share"
          placeholder="请输入分享码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="级别" prop="superior">
        <el-select
          v-model="queryParams.superior"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in superiorOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="登陆状态" prop="canlogin">
        <el-select
          v-model="queryParams.canlogin"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in canloginOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="提现状态" prop="canatm">
        <el-select v-model="queryParams.canatm" clearable placeholder="请选择">
          <el-option
            v-for="item in canatmOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="注册时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          type="daterange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="支付时间" prop="createTime">
        <DatePicker v-model="ctime" :timevalue1="ctime"></DatePicker>
      </el-form-item>
      <el-form-item style="display: block">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ds:user:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ds:user:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ds:user:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ds:user:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会员信息" align="left">
        <template slot-scope="scope">
          <div>
            <div>UID: {{ scope.row.uid }}</div>
            <div>用户姓名: {{ scope.row.realName }}</div>
            <div>用户手机: {{ scope.row.phone }}</div>
            <div>级别: 游客</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="市场信息"
        class-name="small-padding fixed-width"
        width="300"
        align="left"
      >
        <template slot-scope="scope">
          <div>
            <div>好友: &nbsp;{{ scope.row.share }}</div>
            <div>累计消费: &nbsp;{{ scope.row.totalOrder || "0" }}</div>
            <div>
              总业绩: &nbsp;{{
                Number.parseFloat(scope.row.communityPerformance) +
                  Number.parseFloat(scope.row.regionalPerformance) || 0
              }}
            </div>
            <div>大区业绩: &nbsp;{{ scope.row.regionalPerformance || 0 }}</div>
            <div>小区业绩: &nbsp;{{ scope.row.communityPerformance || 0 }}</div>
            <div>好友人数: &nbsp;{{ scope.row.netBodyNumber || 0 }}</div>
          </div>
        </template>
      </el-table-column>

      <el-table-column
        label="账户资金"
        class-name="small-padding fixed-width"
        width="350"
        align="left"
      >
        <template slot-scope="scope">
          <div>
            <div>积分余额: &nbsp;{{ scope.row.money || 0 }}</div>
            <div>GTFY: &nbsp;{{ scope.row.balanceQuota || 0 }}</div>
            <div>等待释放: &nbsp;{{ scope.row.notReleased || 0 }}</div>
          </div>
        </template>
      </el-table-column>

      <el-table-column
        label="开通情况"
        class-name="small-padding fixed-width"
        align="left"
      >
        <template slot-scope="scope">
          <div>
            <div>注册时间: &nbsp;{{ scope.row.createTime || "" }}</div>
            <div>
              是否可登录:
              <!-- <i class="el-icon-check" v-if="scope.row.canlogin==1" @click="updatecan(scope.row,'canlogin',0)"/>
              <i class="el-icon-close" v-else @click="updatecan(scope.row,'canlogin',1)"/> -->

              <el-switch
                v-model="scope.row.canlogin"
                active-color="#ff4949"
                inactive-color="#13ce66"
                :active-value="2"
                :inactive-value="1"
                @change="canLogin($event, scope.row)"
              >
              </el-switch>
            </div>
            <div>
              是否可提现:
              <!-- <i class="el-icon-check" v-if="scope.row.canatm==1"  @click="updatecan(scope.row,'canatm',0)"/>
              <i class="el-icon-close" v-else @click="updatecan(scope.row,'canatm',1)"/> -->
              <el-switch
                v-model="scope.row.canatm"
                active-color="#ff4949"
                inactive-color="#13ce66"
                :active-value="2"
                :inactive-value="1"
                @change="canAtm($event, scope.row)"
              >
              </el-switch>
            </div>
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-row style="margin-bottom: 5%;">
            <el-col :span='12'>
              <el-button type="primary" v-hasPermi="['ds:user:edit']" @click="handleUpdate(scope.row)">修改</el-button>
            </el-col>
            <el-col :span='12'>
              <el-button type="danger" v-hasPermi="['ds:user:remove']" @click="handleDelete(scope.row)">删除</el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span='12'>
              <el-button type="warning" v-if="scope.row.canlogin==1" @click="canLogin(2, scope.row)">冻结</el-button>
              <el-button type="info" v-else disabled>冻结</el-button>
            </el-col>
            <el-col :span='12'>
              <el-button type="success" v-if="scope.row.canatm==1" @click="canAtm(2, scope.row)">提现</el-button>
              <el-button type="info" v-else disabled>提现</el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        style="margin: 0 auto"
      >
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="分享码" prop="share">
          <el-input v-model="form.share" placeholder="请输入分享码" />
        </el-form-item>
        <el-form-item label="上级列表" prop="superior">
          <el-input
            disabled="disabled"
            v-model="form.superior"
            placeholder="请输入上级"
          />
        </el-form-item>
        <el-form-item label="邀请人" prop="invitee">
          <el-input v-model="form.invitee" placeholder="请输入邀请人" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.nickName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  exportUser,
} from "@/api/ds/user";
// import { dataQuery } from "@/api/ds/productOrder";
import ImageUpload from "@/components/ImageUpload";
import DatePicker from "@/components/DatePicker";
import valid from "../../../utils/validator";

import {
  queryTotalPerformanceAndCashSales,
  queryActualPaymentAndContribution,
} from "@/api/ds/index";

export default {
  name: "User",
  components: {
    ImageUpload,
    DatePicker,
  },
  data() {
    return {
      choiceDate0: "",
      pickerOptions: {
        // 设置不能选择的日期
        onPick: ({ maxDate, minDate }) => {
          this.choiceDate0 = minDate.getTime();
          if (maxDate) {
            this.choiceDate0 = "";
          }
        },
        disabledDate: (time) => {
          let choiceDateTime = new Date(this.choiceDate0).getTime();
          const minTime = new Date(choiceDateTime).setMonth(
            new Date(choiceDateTime).getMonth() - 1
          );
          const maxTime = new Date(choiceDateTime).setMonth(
            new Date(choiceDateTime).getMonth() + 1
          );
          const min = minTime;
          const newDate =
            new Date(new Date().toLocaleDateString()).getTime() +
            24 * 60 * 60 * 1000 -
            1;
          const max = newDate < maxTime ? newDate : maxTime;
          //如果已经选中一个日期 则 返回 该日期前后一个月时间可选
          if (this.choiceDate0) {
            return time.getTime() < min || time.getTime() > max;
          }
          //若一个日期也没选中 则 返回 当前日期以前日期可选
          return time.getTime() > newDate;
        },
      },
      // 遮罩层
      loading: true,
      ctime: "", //搜索框 时间选择器数据
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户信息表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 3,
        uid: null,
        openid: null,
        nickName: null,
        avatarImage: null,
        userName: null,
        password: null,
        gender: null,
        share: null,
        idCard: null,
        bankNo: null,
        superior: null,
        invitee: null,
        inviteeSuperior: null,
        createTime: null,
        phone: null,
        realName: null,
        address: null,
        quota: null,
        balanceQuota: null,
        cash: null,
        status: null,
        canatm: null,
        canlogin: null,
        regionalPerformance: null, // 大区业绩
        communityPerformance: null, // 小区业绩
      },
      // 表单参数
      form: {},
      superiorOptions: [
        // 级别下拉框
        { value: "0", label: "游客" },
        { value: "1", label: "一级" },
        { value: "2", label: "二级" },
      ],
      canatmOptions: [
        { value: 1, label: "可提现" },
        { value: 2, label: "不可提现" },
      ],
      canloginOptions: [
        { value: 1, label: "可登陆" },
        { value: 2, label: "不可登陆" },
      ],
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
        ],
        share: [{ required: true, message: "分享码不能为空", trigger: "blur" }],
        phone: [
          {
            required: true,
            validator: valid.telphone,
            message: "",
            trigger: ["change", "blur"],
          },
        ],
      },
      productList: [], // 产品数据
    };
  },
  created() {
    this.getProductData();

    this.getList();
  },
  methods: {
    updatecan(row, type, status) {
      var data = {};
      if (type == "canlogin") {
        data.canlogin = status;
      } else {
        data.canatm = status;
      }
      updateUser(data).then((response) => {
        this.msgSuccess("更新成功");

        this.getList();
      });
    },
    /** 是否可登陆 */
    canLogin(e, row) {
      row.canlogin = e;
      updateUser(row).then((response) => {
        this.msgSuccess("更新成功");
        this.getList();
      });
    },
    /** 是否可提现 */
    canAtm(e, row) {
      // let data = { canlogin: e , id : id}
      row.canatm = e;
      updateUser(row).then((response) => {
        this.msgSuccess("更新成功");
        this.getList();
      });
    },
    /** 查询用户信息列表 */
    async getProductData() {
      // dataQuery().then((response) => {
      //   this.productList = JSON.parse(JSON.stringify(response)).data;
      //   this.loading = false;
      //   this.show = true;
      // });

      let res1 = await queryTotalPerformanceAndCashSales();

      if (res1.code === 200) {
        this.productList = res1.data;
      }

      let res2 = await queryActualPaymentAndContribution();
      if (res2.code === 200) {
        this.productList = Object.assign(this.productList, res2.data);
        this.$forceUpdate();
      }

      this.loading = false;
      this.show = true;
    },
    /** 查询用户信息列表 */
    getList() {
      this.loading = true;
      // if(this.ctime) {
      //   this.queryParams.createTime = this.ctime[0]
      //   this.queryParams.endTime = this.ctime[1]
      // }else {
      //   this.queryParams.createTime = ''
      //   this.queryParams.endTime = ''
      // }
      let q = this.addDateRange(this.queryParams, this.ctime, "");

      listUser(q).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uid: null,
        openid: null,
        nickName: null,
        avatarImage: null,
        userName: null,
        password: null,
        gender: null,
        share: null,
        superior: null,
        invitee: null,
        inviteeSuperior: null,
        phone: null,
        realName: null,
        address: null,
        quota: null,
        balanceQuota: null,
        cash: null,
        status: "0",
        remark: null,
        createTime: null,
        canatm: null,
        canlogin: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.uid);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户信息";
    },
    /** 冻结or提现 */
    updateInfo(row, type) {
      this.$confirm("此操作将修改状态, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then((res) => {
        if (type == "1") {
          // 冻结
          this.canLogin("2", row.uid);
        } else if (type == "2") {
          //提现
          this.canAtm("2", row.uid);
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uid = row.uid || this.ids;
      getUser(uid).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.uid != null) {
            updateUser(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uids = row.uid || this.ids;
      this.$confirm(
        '是否确认删除用户信息编号为"' + uids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delUser(uids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有用户信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportUser(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>

<style scoped>
.el-form-item {
  margin-bottom: 0.5%;
}
.flex {
  display: flex;
  align-items: center;
}
.el-icon-check {
  font-size: 24px;
  color: green;
}
.el-icon-close {
  font-size: 24px;
  color: red;
}
.titleClass {
  background: lightgray;
  text-align: center;
  font-size: 18px;
  padding: 1%;
}
.titleContentClass {
  text-align: center;
  color: red;
  border: 1px solid lightgray;
  font-size: 18px;
  padding: 1%;
}
</style>
